package com.cjg.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class reSyn2 {
    public static void main(String[] args) {
        final ticket2 ticket = new ticket2();
        new Thread(()->{ for (int i = 0; i <20 ; i++) ticket.buy();},"A").start();
        new Thread(()->{ for (int i = 0; i <20 ; i++) ticket.buy();},"C").start();
        new Thread(()->{ for (int i = 0; i <20 ; i++) ticket.buy();},"B").start();
    }



}
class ticket2{
    private int tickets=50;
 Lock lock =new ReentrantLock();
    //核心是 队列加🔒
    public  void buy(){
        //加锁
        lock.lock();
        try {
            if (tickets>0)
                System.out.println(Thread.currentThread().getName()+"卖出了第"+tickets--+"票");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}